package edu.cs.usask.ca.silver.businesslogic.commands.generic;

import java.io.File;

import edu.cs.usask.ca.silver.api.businesslogic.AssociatedFileI;
import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.businesslogic.ids.FileID;
import edu.cs.usask.ca.silver.businesslogic.ids.ModelProjectID;
import edu.cs.usask.ca.silver.businesslogic.ids.ModelVersionID;
import edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID;
import edu.cs.usask.ca.silver.businesslogic.ids.ScenarioID;
import edu.cs.usask.ca.silver.businesslogic.util.SilverAutoManagedObjectFactory;

public class AssociatedFile implements AssociatedFileI {

	private static final SilverAutoManagedObjectFactory fileFactory = new SilverAutoManagedObjectFactory(
			"edu.cs.usask.ca.silver.businesslogic.commands.generic.AssociatedFile");

	public FileID createFile(String fName, String fDescr,
			String fileSourceLocation, StorageI storage) throws Exception {
		FileID result = new FileID(storage.addFile(fName,
				fDescr, fileSourceLocation));
		return result;
	}

	public String getName(FileID fId, StorageI storage) throws Exception {
		return storage.getFName(fId.getUUIdVal());
	}

	public String getDescription(FileID fId, StorageI storage) throws Exception {
		return storage.getFDescription(fId.getUUIdVal());
	}

	public void getFileToLocalStorage(FileID fId, String destination, StorageI storage)
			throws Exception {
		File output = new File(destination);
		storage.getFFileToLocalStorage(fId.getUUIdVal(),
				output);
	}

	public boolean setDescription(FileID fId, String description, StorageI storage)
			throws Exception {
		return storage.setFDescription(fId.getUUIdVal(),
				description);
	}

	public void addFileToScenarioAsInputFile(FileID fId, ScenarioID sId, StorageI storage)
			throws Exception {
		storage.addFFileToScenarioAsInputFile(
				fId.getUUIdVal(), sId.getUUIdVal());
	}

	public void addFileToScenarioAsTrajectoryFile(FileID fId, ScenarioID sId, StorageI storage)
			throws Exception {
		storage.addFFileToScenarioAsTrajectoryFile(
				fId.getUUIdVal(), sId.getUUIdVal());
	}

	public void addFileToScenarioAsAnalysisFile(FileID fId, ScenarioID sId, StorageI storage)
			throws Exception {
		storage.addFFileToScenarioAsAnalysisFile(
				fId.getUUIdVal(), sId.getUUIdVal());
	}

	public void addFileToModelProject(FileID fId, ModelProjectID mpId, StorageI storage)
			throws Exception {
		storage.addFileToModelProject(fId.getUUIdVal(),
				mpId.getUUIdVal());
	}

	public void addFileToModelVersion(FileID fId, ModelVersionID mvId, StorageI storage)
			throws Exception {
		storage.addFileToModelVersion(fId.getUUIdVal(),
				mvId.getUUIdVal());
	}

	public void addFileToScenarioCollection(FileID fId,
			ScenarioCollectionID scId, StorageI storage) throws Exception {
		storage.addFileToScenarioCollection(
				fId.getUUIdVal(), scId.getUUIdVal());
	}

	public void addFileToScenario(FileID fId, ScenarioID sId, StorageI storage) throws Exception {
		storage.addFileToScenario(fId.getUUIdVal(),
				sId.getUUIdVal());
	}

	public boolean deleteFile(FileID fId, StorageI storage) throws Exception {
		return storage.deleteFile(fId.getUUIdVal());
	}

	public boolean backupDB(String destination, StorageI storage) throws Exception {
		return false;
	}

	public static SilverAutoManagedObjectFactory getObjectPoolFactory() {
		return fileFactory;
	}

}
